﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Mortgage.DAL.Entity;
using System.Globalization;
using Microsoft.Reporting.WebForms;

namespace Mortgage
{
    public static class Utility
    {

        const string MORT_STATE = "MORT_STATE";

        const string INTE_STATE = "INTE_STATE";

        public static string FormatDateTime(DateTime value)
        {
            //return value.ToString(Mortgage.Properties.Settings.Default.SrvDateTimeFormat);
            return value.ToShortDateString();
        }

        public static string FormatMoney(decimal money)
        {
            //return value.ToString(Mortgage.Properties.Settings.Default.SrvDateTimeFormat);
            return money.ToString("0,0 VND", CultureInfo.InvariantCulture);
        }


        public static int AgentId
        {
            get
            {
                int result = 0;

                if (HttpContext.Current.Session["__AGENTID"] != null)
                    result = int.Parse(HttpContext.Current.Session["__AGENTID"].ToString());
                else
                {
                    System.Web.Security.FormsAuthentication.RedirectToLoginPage();
                }
                return result;
            }
            set
            {
                HttpContext.Current.Session["__AGENTID"] = value;
            }
        }

        public static int GetDefaultAgentID()
        {
            int result = 0;
            Mortgage.Srv.IAgentSrv svc = Mortgage.Srv.SrvFactory.CreateAgentSrv();

            tAgent defAgent = svc.GetAll().Cast<tAgent>().FirstOrDefault();
            if (defAgent != null)
            {
                Utility.AgentId = defAgent.ID;
                result = defAgent.ID;
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="outValue"></param>
        /// <returns>
        /// 0 : success
        /// 1 : error 
        /// </returns>
        public static int ParseDateTime(string value, out DateTime outValue)
        {
            int result = 0;
            outValue = DateTime.MinValue;
            try
            {
                outValue = Convert.ToDateTime(value);
                //outValue = DateTime.ParseExact(value, Mortgage.Properties.Settings.Default.SrvDateTimeFormat, null);
            }
            catch (Exception)
            {
                result = 1;
            }
            return result;
        }

        public static List<tzConfig> GetMortgageStatus()
        {
            if (System.Web.HttpContext.Current.Application[MORT_STATE] == null)
                RestMortgageStatus();
            return System.Web.HttpContext.Current.Application[MORT_STATE] as List<tzConfig>;

        }

        public static List<vwInterestRate> GetInterestItem()
        {
            if (System.Web.HttpContext.Current.Application[INTE_STATE] == null)
                ResetInterstItem();
            return System.Web.HttpContext.Current.Application[INTE_STATE] as List<vwInterestRate>;

        }

        public static string GetMorgageStatusCode(int id)
        {
            string result = string.Empty;
            List<tzConfig> mortStatus = GetMortgageStatus();
            if (mortStatus != null)
                result = mortStatus.Where(m => m.ID == id).Select(i => i.Value).FirstOrDefault();
            return result;
        }

        public static vwInterestRate GetInterestCode(int id)
        {
            vwInterestRate result = null;
            List<vwInterestRate> inters = GetInterestItem();
            if (inters != null)
                result = inters.Where(m => m.ID == id).FirstOrDefault();
            return result;
        }

        public static void RestMortgageStatus()
        {
            Mortgage.Srv.IConfigSrv cfgSrv = Mortgage.Srv.SrvFactory.CreateConfigSrv();
            System.Web.HttpContext.Current.Application[MORT_STATE] = cfgSrv.GetAll(MORT_STATE);
        }

        public static void ResetInterstItem()
        {
            Mortgage.Srv.IInterestRateSrv cfgSrv = Mortgage.Srv.SrvFactory.CreateInterestRateSrv();
            System.Web.HttpContext.Current.Application[INTE_STATE] = cfgSrv.GetAllInterestInfo();
        }

    }
}